package org.devocean.groupbuying.utils;

import org.apache.log4j.Logger;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class ServiceLogger {

	private final static Logger LOGGER = Logger.getLogger("Service");

	@Before("execution(* org.devocean.groupbuying.service.*.*(..))")
	public void before(JoinPoint joinPoint) {

		Object[] args = joinPoint.getArgs();
		String claz = joinPoint.getTarget().getClass().getSimpleName();
		String name = joinPoint.getSignature().getName();

		StringBuffer sb = new StringBuffer(claz + ".");
		sb.append(name + "(");

		for (int i = 0; i < args.length; i++) {
			Object o = args[i];
			sb.append("'" + o + "'");
			sb.append((i == args.length - 1) ? "" : ", ");
		}
		sb.append(")");
		sb.append(" executing ...");
		LOGGER.info(sb);

	}

	// @Around("execution(* org.devocean.groupbuying.service.*.*(..))")
	// public Object arround(ProceedingJoinPoint joinPoint) throws Throwable {
	// Object[] args = joinPoint.getArgs();
	// String claz = joinPoint.getTarget().getClass().getSimpleName();
	// String name = joinPoint.getSignature().getName();
	//
	// StringBuffer sb = new StringBuffer(claz + ".");
	// sb.append(name + "(");
	//
	// for (int i = 0; i < args.length; i++) {
	// Object o = args[i];
	// sb.append("'" + o + "'");
	// sb.append((i == args.length - 1) ? "" : ", ");
	// }
	// sb.append(")");
	// LOGGER.info(sb);
	// Object returnObject = joinPoint.proceed();
	// System.out.println("After");
	// return returnObject;
	// }

	@AfterReturning("execution(* org.devocean.groupbuying.service.*.*(..))")
	public void after(JoinPoint joinPoint) throws Throwable {
		Object[] args = joinPoint.getArgs();
		String claz = joinPoint.getTarget().getClass().getSimpleName();
		String name = joinPoint.getSignature().getName();

		StringBuffer sb = new StringBuffer(claz + ".");
		sb.append(name + "(");

		for (int i = 0; i < args.length; i++) {
			Object o = args[i];
			sb.append("'" + o + "'");
			sb.append((i == args.length - 1) ? "" : ", ");
		}
		sb.append(")");
		sb.append(" succussfuly executed");
		LOGGER.info(sb);
	}

	@AfterThrowing("execution(* org.devocean.groupbuying.service.*.*(..))")
	public void efterThrow(JoinPoint joinPoint) throws Throwable {
		Object[] args = joinPoint.getArgs();
		String claz = joinPoint.getTarget().getClass().getSimpleName();
		String name = joinPoint.getSignature().getName();

		StringBuffer sb = new StringBuffer(claz + ".");
		sb.append(name + "(");

		for (int i = 0; i < args.length; i++) {
			Object o = args[i];
			sb.append("'" + o + "'");
			sb.append((i == args.length - 1) ? "" : ", ");
		}
		sb.append(")");
		sb.append(" threw an Exxception");
		LOGGER.error(sb);
	}

}
